package com.jari.lg.service.profile.cross;

import com.jari.lg.domain.*;
import com.jari.lg.mapper.CrossDeviceDataRTMapper;
import com.jari.lg.mapper.JamIndexHisHourMapper;
import com.jari.lg.mapper.JamIndexRTMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.LogUtil;
import com.jari.lg.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ctl
 * @Description
 * @create 2022-03-24 14:07
 */
@Service
@Slf4j
public class CrossProfileService {

    @Resource
    private JamIndexHisHourMapper jamIndexHisHourMapper;
    @Resource
    private CrossDeviceDataRTMapper crossDeviceDataRTMapper;
    @Resource
    private JamIndexRTMapper jamIndexRTMapper;

    /**
     * 路口画像（7）-拥堵对比
     */
    @Scheduled(cron = "0 0 * * * ? ")
    public void createJamIndexHisHour() {
        createJamIndexHisHourFromDate(new Date());
    }

    /**
     * 根据传入的时间生成交叉口拥堵天表一天每小时数据
     *
     * @param date
     */
    public void createJamIndexHisHourFromDate(Date date) {
        try {
            List<JamIndexHisHour> resultList = new ArrayList();
            JamIndexHisHour result = new JamIndexHisHour();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.HOUR_OF_DAY, -1);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            String keyAreaCrossType = BaseData.crossTypeList.stream()
                    .filter(x -> x.getDictlabel().equals(BaseData.crossType[6])).findFirst().get().getDictdata();
            for (int i = 0; i < BaseData.crossBaseList.size(); i++) {
                CrossBase crossBase = BaseData.crossBaseList.get(i);
                if (!keyAreaCrossType.equals(crossBase.getCrosstype())) {
                    result.setCrossid(crossBase.getCrosscode());
                    result.setCrossname(crossBase.getCrossname());
                    result.setMaxqueue(new Random().nextInt(200) + 10.0);
                    result.setDatetime(c.getTime());

                    resultList.add(result);
                    result = new JamIndexHisHour();
                }

            }
            jamIndexHisHourMapper.insertBatch(resultList);
        } catch (Exception exception) {
            log.error(LogUtil.getStackMsg(exception));
        }
    }

    /**
     * 按月生成交叉口拥堵天表
     */
    public void batchJamIndexHisHourPerMonth(Integer year, Integer month) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month - 1);    //month内部是从0开始
        Integer maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        //天
        for (int i = 0; i < maxDay; i++) {
            c.set(Calendar.DAY_OF_MONTH, i + 1);
            //小时
            for (int j = 0; j < 24; j++) {
                c.set(Calendar.HOUR_OF_DAY, j);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                Date date = c.getTime();
                createJamIndexHisHourFromDate(date);
            }
        }
    }

    /**
     * 路口画像（7）-数据采集水平
     */
    @Scheduled(cron = "0 0 * * * ? ")
    public void createCrossDeviceDataRT() {
        try {

            crossDeviceDataRTMapper.deleteByExample(null);

            List<CrossDeviceDataRT> resultList = new ArrayList();
            CrossDeviceDataRT result = new CrossDeviceDataRT();

            String keyAreaCrossType = BaseData.crossTypeList.stream()
                    .filter(x -> x.getDictlabel().equals(BaseData.crossType[6])).findFirst().get().getDictdata();
            for (int i = 0; i < BaseData.crossBaseList.size(); i++) {
                CrossBase crossBase = BaseData.crossBaseList.get(i);
                if (!keyAreaCrossType.equals(crossBase.getCrosstype())) {
                    for (int j = 0; j < BaseData.deviceTypeList.size(); j++) {
                        result.setUuid(Utils.getUUID());
                        result.setCrossid(crossBase.getCrosscode());
                        result.setCrossname(crossBase.getCrossname());
                        SysDictData sysDictData = BaseData.deviceTypeList.get(j);
                        result.setDatatype("文本");
                        result.setDevicetype(sysDictData.getDictlabel());
                        result.setOriginaldatacount((int) (System.currentTimeMillis() / 100000 / sysDictData.getDictsort()));
                        result.setCleandatacount((int) (result.getOriginaldatacount() * 0.999));
                        result.setDatestamp(new Date());
                        resultList.add(result);
                        result = new CrossDeviceDataRT();
                    }
                }
            }
            crossDeviceDataRTMapper.insertBatch(resultList);
        } catch (Exception exception) {
            log.error(LogUtil.getStackMsg(exception));
        }
    }

    /**
     * 路口画像（7）-交叉口拥堵指数
     */
    @Scheduled(cron = "0 0 * * * ? ")
    public void createJamIndexRT() {
        try {

            jamIndexRTMapper.deleteByExample(null);

            List<JamIndexRT> resultList = new ArrayList();
            JamIndexRT result = new JamIndexRT();
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.HOUR_OF_DAY, -1);
            c.add(Calendar.MINUTE, 0);
            c.add(Calendar.SECOND, 0);
            String keyAreaCrossType = BaseData.crossTypeList.stream()
                    .filter(x -> x.getDictlabel().equals(BaseData.crossType[6])).findFirst().get().getDictdata();
            for (int i = 0; i < BaseData.crossBaseList.size(); i++) {
                CrossBase crossBase = BaseData.crossBaseList.get(i);
                if (!keyAreaCrossType.equals(crossBase.getCrosstype())) {
                    result.setCrossid(crossBase.getCrosscode());
                    result.setCrossname(crossBase.getCrossname());
                    result.setMaxqueue(new Random().nextInt(200) + 10.0);
                    result.setDatetime(c.getTime());

                    resultList.add(result);
                    result = new JamIndexRT();
                }

            }
            jamIndexRTMapper.insertBatch(resultList);
        } catch (Exception exception) {
            log.error(LogUtil.getStackMsg(exception));
        }
    }


}
